package com.musemun.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.musemun.contants.StaticVariable;

import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
//import com.sun.org.apache.xml.internal.security.utils.Base64;
import org.springframework.context.annotation.Configuration;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;

/**
 * @description:
 *     二维码的生成工具类：下面是一个在controller返回二维码的例子
 *
 *  GetMapping("test")
 *  public String test(HttpServletResponse response) throws WriterException, IOException
 *  {
 *         return TwoCodeUtil.generateCodeBufferImage("https://www.baidu.com/");
 *  }
 *
 * @author winner
 * @date 2020/4/12
 */
@Component
public class TwoCodeUtil{

    private static HashMap<EncodeHintType,Comparable>HINTS=null;
    static{
        HINTS=new HashMap<>();
        HINTS.put(EncodeHintType.CHARACTER_SET,StaticVariable.ENCODING_TYPE);
        //二维码的纠错等级
        HINTS.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        //设置图片边距
        HINTS.put(EncodeHintType.MARGIN,2);
    }
    /**
     * description:
     *
     * @param url 需要生成二维码的url
     * @return java.lang.String 图片的base64压缩String
     */

    public static String generateCodeBufferImage(String url) throws WriterException, IOException {
        ByteArrayOutputStream os=new ByteArrayOutputStream();
        QRCodeWriter qrCodeWriter=new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, StaticVariable.TOW_IMAGE_WIDTH, StaticVariable.TWO_IMAGE_HEIGHT,HINTS);
        MatrixToImageWriter.writeToStream(bitMatrix,StaticVariable.IMAGE_TYPE,os);
        return StaticVariable.BASE64_TYPE + Base64.encodeBase64String(os.toByteArray());
    }

    /**
     *
     *     给定id返回查询的字节流
     * @param id  需要的id
     */
    public static void generateCodeBufferImage(long id, HttpServletResponse response) throws IOException, WriterException {
        //        MatrixToImageWriter.writeToStream(bitMatrix,"png",response.getOutputStream());
        QRCodeWriter qrCodeWriter=new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(StaticVariable.URL+"/two_dimensional_code/by_image/"+id, BarcodeFormat.QR_CODE, StaticVariable.TOW_IMAGE_WIDTH, StaticVariable.TWO_IMAGE_HEIGHT,HINTS);
        response.setContentType("image/png");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        MatrixToImageWriter.writeToStream(bitMatrix,"png",response.getOutputStream());
    }

    /**
     *
     *     给定id返回查询的字节流
     * @param url  需要的url
     */
    public static void generateCodeBufferImage(String url, HttpServletResponse response) throws IOException, WriterException {
        //        MatrixToImageWriter.writeToStream(bitMatrix,"png",response.getOutputStream());
        QRCodeWriter qrCodeWriter=new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, StaticVariable.TOW_IMAGE_WIDTH, StaticVariable.TWO_IMAGE_HEIGHT,HINTS);
        response.setContentType("image/png");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        MatrixToImageWriter.writeToStream(bitMatrix,"png",response.getOutputStream());
    }

}
